<?php
  //============================================================================
  // ____________________________PLIGG TEAM ADDON_______________________________
  //
  // This CLASS was created by AnAlienHolakres3 ||| XXXXX  XX     XX XXXX   XXXX
  // (c) 2007-2008                Jakub Holasek ||| XXXXX  XX     XX X      X
  // @last_update                    07/02/2008 ||| XX     XX     XX X  XX  X  XX
  // @version                              0.15 ||| XX     XXXXXX XX XXXXX  XXXXX
  //
  // This file can be used by Pligg community.
  //=============================================================================
  
  // Class Team member contains general information about team members.
  // It means team member is normal user
  
  class Team_Member
  {
      private $team_id = 0;
      private $user_id = 0;
      private $user_name = '';
      
      private $membership_data;
      private $membership_user_data;
      private $read = true;
      
      //============================================================================
      //  Function read_members() gets Team Members data from database(id of user and name)
      //  Members without team creators;
      //============================================================================
      function read_members()
      {
          global $db;
          
          $team_id = $this->team_id;
          
          if (($membership = $db->get_results("SELECT " . table_membership . ".user_id,user_login FROM " . table_membership . "
         JOIN " . table_users . " ON " . table_users . ".user_id=" . table_membership . ".user_id
         WHERE team_id= $team_id AND " . table_membership . ".user_id NOT IN (SELECT team_creator_id FROM " . table_teams . " WHERE team_id=$team_id)"))) {
              $this->membership_data = $membership;
              
              
              $this->read = true;
              return true;
          }
          $this->read = false;
          return false;
      }
      
      //============================================================================
      //  Function read_membership() gets all teams (names and objectives
      //   in which is user member or admin      
      //============================================================================
      function read_membership()
      {
          global $db,$current_user;
          $user_id = $current_user->user_id;
          
          if ($membership_ud = $db->get_results("SELECT  
                                                    " . table_membership . ".user_id,                                                    
                                                    " . table_teams . ".team_name,
                                                    " . table_teams . ".team_objective
                                              FROM "
                                                    . table_teams . "
                                                    JOIN 
                                                         " . table_membership 
                                                         . "ON" .
                                                         table_membership."team_id=". table_teams . "team_id
                                                         AND"
                                                         . table_membership .".user_id=$user_id")) {
                                                             
                $this->membership_user_data=$membership_ud;                                                                                                   
                $this->read = true;
                return true;   
              
          }
                $this->read = false;
                return false;
          
          
      }
      //============================================================================
      //  Function store() saves membership data in database
      //============================================================================
      
      function store()
      {
          global $db;
          
          $team_id = $this->team_id;
          $user_id = $this->user_id;
          
          
          if ($user_id === 0) {
              $user_id = $this->get_user_id();
          }    
                            
             if( $db->query("INSERT INTO " . table_membership . " (team_id, user_id) VALUES ($team_id, $user_id)")){
                return true; 
                  
             }  return false;;                            
              
          
      }
      
      //============================================================================
      //  Function is_duplicate_membership() finds out if user is already in team
      //  @return true if membership is already in database otherwise false
      //============================================================================
      
      function is_duplicate_membership()
      {
          global $db;
          
          $team_id = $_POST['team_id'];
          $user_id = $this->get_user_id();
          
          $n = $db->get_var("SELECT count(*) FROM " . table_membership . " WHERE team_id= $team_id AND user_id = $user_id AND user_id NOT IN
                             (SELECT team_creator_id FROM " . table_teams . " WHERE team_id=$team_id)");
          if ($n > 0) {
              return true;
          }
          return false;
      }
      
      //============================================================================
      //  Function fill_print() assign team members smartys
      //============================================================================
      
      function fill_print()
      {
          global $main_smarty;
          
          $members_data = $this->membership_data;
          
          if ($members_data) {
              foreach ($members_data as $data_member) {
                  $last_username = $data_member->user_login;
                  $members_id[] = $data_member->user_id;    
                  $members_name[] = $data_member->user_login;
                  $members_url[] = getmyurl('user2', $data_member->user_login, 'profile');
                  $members_message_url[] = my_pligg_base . '/module.php?module=simple_messaging&view=compose&return=' . 
                                           urlencode($_SERVER['REQUEST_URI']) .'&to=' .$data_member->user_login;
              } 
              $main_smarty->assign('last_team_name', $last_username);
              $main_smarty->assign('team_members_id', $members_id);
              $main_smarty->assign('team_members_name', $members_name);
              $main_smarty->assign('team_members_url', $members_url);
              $main_smarty->assign('team_members_message_url',$members_message_url); 
              
              
          }
      }
      
      //============================================================================
      //  Function check_team_member_name_length() checks if username was entered correctly
      // @return true if username is at least 3 CHARS long
      //============================================================================
      
      function check_team_member_name_length()
      {   $team = new Team;
          if ($team->strlen_utf8($this->get_user_name($_POST["member_name"])) < 3) {
              return false;
          }
          return true;
      }
      //============================================================================
      //  Function check_member_id() checks if team member is member of team
      // @return true if it is otherwise false
      //============================================================================ 
      function check_member_id($member_id)
      {
          $this->read_members();
          $members = $this->get_team_members();
             
          if ($members) {
              foreach ($members as $member) {
                  if ($member->user_id == $member_id) {
                      return true;
                  }
              }
          }
          return false;
      }
      //============================================================================
      //  Function check_user_exists() checks if username is real user name and exists
      //============================================================================
      
      function check_user_exist()
      {
          global $db;
          
          $name = $this->get_user_name($_POST["member_name"]);
           
          $count = $db->get_var("SELECT count(user_id) FROM " . table_users . " WHERE user_login LIKE '$name'");
          if ($count) {
              return true;
          }
          return false;
      }
      
      //============================================================================
      //  Function delete_member() deletes team member from database
      //  When team member is deleted, his task IS DELETED ALSO!
      //============================================================================
      
      function delete_member()
      {
          global $db;
          
          $user_id = $this->user_id;
          $team_id = $this->team_id;                
          
          if ($db->query("DELETE FROM " . table_membership . " WHERE user_id=$user_id AND team_id=$team_id")) {
              return true;
          }
          return false;
      }
      
      
      
      //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      //  VARIABLE GET FUNCTIONS                                                                                                               X
      //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      
      //==============================================================================
      //  Function get_team_members()  returns array of team members data
      //=============================================================================
      function get_team_members()
      {
          return $this->membership_data;
      }
      //============================================================================
      //  Function get_team_id()  returns team id
      //============================================================================
      function get_team_id()
      {
          return $this->team_id;
      }
      //============================================================================
      //  Function get_member_id()  returns user id
      //============================================================================
      function get_member_id()
      {
          return $this->user_id;
      }
      
      //============================================================================
      //  Function get_user_name()  gets cleared user name
      // @param user_name
      //============================================================================
      function get_user_name($user_name)
      {
          global $db;
          $cleared_name = $db->escape(strip_tags(trim($user_name)));
          return $cleared_name;
      }
      
      //============================================================================
      //  Function get_user_id()  returns user id
      //============================================================================
      function get_user_id()
      {
          global $db;
          
          $name = $this->get_user_name($_POST["member_name"]);
          $user_id = $db->get_var("SELECT user_id FROM " . table_users . " WHERE user_login LIKE '$name'");
          return $user_id;
      }
      
      //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      //  VARIABLE SET FUNCTIONS                                                                                                               X
      //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      //============================================================================
      //  Function set_team_id()  set team id
      //============================================================================
      function set_team_id($team_id)
      {
          $this->team_id = $team_id;
      }
      //============================================================================
      //  Function set_user_id()  sets user id
      //============================================================================
      function set_user_id($user_id)
      {
          $this->user_id = $user_id;
      }
      
      //============================================================================
      //  Function set_user_name()  set user name
      //============================================================================
      function set_user_name($user_name)        
      {
          global $db;    
          
          $this->user_name = $db->escape(trim(strip_tags($user_name)));
      }
  }
?>